package com.gotrade.demo.advice;

import com.gotrade.demo.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author jason.tang
 * @create 2018-09-21 15:34
 * @description 控制层业务异常处理
 */

@ControllerAdvice(basePackages = {"com.gotrade.demo.controller"})
public class ModuleControllerAdvice {

    public static final Logger LOGGER = LoggerFactory.getLogger(ModuleControllerAdvice.class);

    public static final Logger SERVICE_LOGGER = LoggerFactory.getLogger(ServiceException.class);

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(ServiceException.class)
    private String handleServiceException(ServiceException exception) {
        String message = new StringBuffer("业务受理失败原因：").append(exception.getLocalizedMessage()).toString();
        SERVICE_LOGGER.info(message);
        return "{\"code\": 500, \"message\":" + message + "}";
    }
}